﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Boom.Navigation;

namespace Boom.GameObjects
{
    public class MonsterBoss:MultilFrame
    {
        private int _Status;
        private int _Hp;
        private Vector2 _Speed;
        private bool _Move;
        private int _VectorA;
        private SpriteFont _FontBoss;
        
        private TimeSpan _ElapsedTimeAction;
        private TimeSpan _ElapsedTime;
        private int _IndexBossAction;
        private int _TimeBossAction;
        private int _HoriFrame = 8;
        private int _SkillBlock = 16;
        private int _TalkBlock = 8;
        private int _DeadBlock = 8;
        private int _StatusBlock = 8;
        private int _IndexTalk = 0;
        private bool _ActionTalk;

        public int HP
        {
            get { return _Hp; }
        }

        public int Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private bool ActionTalk
        {
            get { return _ActionTalk; }
            set 
            { 
                if(!value) _IndexTalk++;
                if (_IndexTalk >= Config.StringTalk.Length) _IndexTalk = 0;
                _ActionTalk = value;
            }
        }

        public MonsterBoss(Game Mygame, ref Texture2D TextureBoss, Vector2 Position)
            : base(Mygame, ref TextureBoss, 80, 80)
        {
            _Position = Position;
            _ElapsedTimeAction = TimeSpan.Zero;
            _IndexBossAction = 0;
            _Status = 2;
            _VectorA = 1;
            _Hp = Config.DefaultHpMonsterBoss;
            _TimeBossAction = Config.TimeBossAction;
            _FontBoss = Game.Content.Load<SpriteFont>("Font/FontBoss");
        }

        //Chuyển frame trong khoảng
        private void ChangeFrame(GameTime gameTime,int MinFrame, int MaxFrame,int TimeChange)
        {
            if (_ActiveFrame < MinFrame || _ActiveFrame >= MaxFrame ) _ActiveFrame = MinFrame;
            _ElapsedTime += gameTime.ElapsedGameTime;
            if (_ElapsedTime > TimeSpan.FromMilliseconds(TimeChange))
            {
                _ElapsedTime -= TimeSpan.FromMilliseconds(TimeChange);
                _ActiveFrame++;
            }
        }

        //Di chuyển qua trái
        private void MoveLeft(GameTime gameTime)
        {
            _Speed = new Vector2(-2, 0);
            ChangeFrame(gameTime, 0, _HoriFrame - 1, 100);
        }

        //Di Chuyển qua phải
        private void MoveRight(GameTime gameTime)
        {
            _Speed = new Vector2(2, 0);
            ChangeFrame(gameTime, _HoriFrame, _HoriFrame * 2 - 1, 100);
        }

        //Di chuyển theo thời gian
        private void Move(GameTime gameTime)
        {
            if (!_Move)
            {
                if (_VectorA == 1 && !_Move)
                {
                    _VectorA = 2;
                    _Move = true;
                }
                if (_VectorA == 2 && !_Move)
                {
                    _VectorA = 1;
                    _Move = true;
                }
            }
            _Move = true;
            if (_VectorA == 1) MoveLeft(gameTime);
            if (_VectorA == 2) MoveRight(gameTime);
        }

        //Boss nói chuyện
        private void Talk(GameTime gameTime)
        {
            _Speed = new Vector2(0, 0);
            ChangeFrame(gameTime, _HoriFrame * 2, _HoriFrame * 3 - 1, 300);
        }

        //Sử dụng skill
        private void ActionSkill(GameTime gameTime)
        {
            _Speed = new Vector2(0, 0);
            ChangeFrame(gameTime, _HoriFrame * 3, _HoriFrame * 5 - 1, 100);
            if (_ActiveFrame == _HoriFrame * 5 - 1) _IndexBossAction = 0;
            
        }

        //Khi bị giảm máu
        private void HpDown(GameTime gameTime)
        {
            _Speed = new Vector2(0, 0);
            ChangeFrame(gameTime, _HoriFrame * 5, _HoriFrame * 6 - 1, 200);
            if (_ActiveFrame == _HoriFrame * 6 - 1)
            {
                _Status = 2;
                _Hp--;
                _Move = false;
            }
        }

        //Khi chết
        private void Dead(GameTime gameTime)
        {
            _Speed = new Vector2(0, 0);
            ChangeFrame(gameTime, _HoriFrame * 6, _HoriFrame * 7 - 1, 200);
            if (_ActiveFrame == _HoriFrame * 7 - 1)
            {
                _Hp--;
                Enabled = false;
            }
        }

        //Thay đổi trạng thái khi bị boom tấn công
        private void UpdateStatus(GameTime gameTime)
        {
            if (_Status == 1)
            {
                HpDown(gameTime);
            }
            if (_Status == 0)
            {
                Dead(gameTime);
            }
        }

        //Giữ trong đối tượng nằm trong màn hình
        private void KeepInBounds()
        {
            if (Position.X < 0)
            {
                _Position.X = 0;
                _Move = false;
            }
            if (Position.X + 80 > 600)
            {
                _Position.X = 520;
                _Move = false;
            }
        }

        //Cập nhật thứ tự hành động
        private void IndexBossAction(GameTime gameTime, int CountStatus)
        {
            _ElapsedTimeAction += gameTime.ElapsedGameTime;
            if (_ElapsedTimeAction > TimeSpan.FromSeconds(_TimeBossAction))
            {
                _ElapsedTimeAction -= TimeSpan.FromSeconds(_TimeBossAction);
                _IndexBossAction++;
                if (_IndexBossAction > CountStatus) _IndexBossAction = 0;
            }
        }

        //Cập nhật hành động
        private void UpdateActionBoss(GameTime gameTime)
        {
            IndexBossAction(gameTime, 3);
            switch (_IndexBossAction)
            {
                case 0:
                    Move(gameTime);
                    break;
                case 1:
                    ActionTalk = true;
                    Talk(gameTime);
                    break;
                case 2:
                    ActionTalk = false;
                    ActionSkill(gameTime);
                    break;    
            }
        }

        public override void Update(GameTime gameTime)
        {
            _Bounds = new Rectangle((int)Position.X, (int)Position.Y, 80, 80);
            if (_Move)
            {
                _Position += _Speed;
            }
            KeepInBounds();
            if (_Status == 2)
            {
                UpdateActionBoss(gameTime);
            }
            UpdateStatus(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (_ActionTalk)
            {
                _SBatch.DrawString(_FontBoss, Config.StringTalk[_IndexTalk], _Position + Config.PositionString, Color.White);
            }
            base.Draw(gameTime);
        }
    }
}
    